figure.Bascula = new Class({
    Extends: figure.Dial,

    initialize: function(id, css, value, unit, min, capacity, step) {
        this.parent(id, css, value, unit, min, capacity, step);
    },

    addEvents: function() {
        var controlElement = this.controlElement;
        controlElement.on('growUp', function growUp(event, nextClasses, previousClasses) {
            if (figure.stopAnimation) {
                window.setTimeout(function() { growUp(event, nextClasses, previousClasses) }, figure.speed);
                return;
            }
            var valueAsInt = parseInt(controlElement[0].value);
            var maxValueAsInt = parseInt(controlElement[0].max);
            var amountEntriesOpen = 0, amountOutputsOpen = 0;
            var i;
            if (valueAsInt < 0.8*maxValueAsInt) {
                for (i = 0; i< previousClasses.length; i++){
                    figure.pipesStatus[previousClasses[i]+'End'] = true;
                    if (figure.isOpen(previousClasses[i])) {
                        amountEntriesOpen++;
                    }
                }
                for (i = 0; i< previousClasses.length; i++){
                    if (figure.isOpen(previousClasses[i])) {
                        valueAsInt += 500/amountEntriesOpen;
                        controlElement[0].value = valueAsInt < maxValueAsInt ? valueAsInt: maxValueAsInt;
                        controlElement.trigger('change');
                    }
                }
                for (i = 0; i< nextClasses.length; i++){
                    figure.pipesStatus[nextClasses[i]+'Start'] = false;
                }
            } else if (valueAsInt < 0.95*maxValueAsInt) {
                for (i = 0; i< previousClasses.length; i++){
                    figure.pipesStatus[previousClasses[i]+'End'] = true;
                    if (figure.isOpen(previousClasses[i])) {
                        amountEntriesOpen++;
                    }
                }
                for (i = 0; i< previousClasses.length; i++){
                    if (figure.isOpen(previousClasses[i])) {
                        valueAsInt += 500/amountEntriesOpen;
                        controlElement[0].value = valueAsInt < maxValueAsInt ? valueAsInt: maxValueAsInt;
                        controlElement.trigger('change');
                    }
                }
                for (i = 0; i< nextClasses.length; i++){
                    figure.pipesStatus[nextClasses[i]+'Start'] = true;
                    if (figure.isOpen(nextClasses[i])) {
                        amountOutputsOpen++;
                    }
                }
                for (i = 0; i< nextClasses.length; i++){
                    if (figure.isOpen(nextClasses[i])) {
                        valueAsInt -= 500/amountOutputsOpen;
                        controlElement[0].value = valueAsInt;
                        controlElement.trigger('change');
                    }
                }
            } else {
                for (i = 0; i< previousClasses.length; i++){
                    figure.pipesStatus[previousClasses[i]+'End'] = false;
                }
                for (i = 0; i< nextClasses.length; i++){
                    figure.pipesStatus[nextClasses[i]+'Start'] = true;
                    if (figure.isOpen(nextClasses[i])) {
                        amountOutputsOpen++;
                    }
                }
                for (i = 0; i< nextClasses.length; i++){
                    if (figure.isOpen(nextClasses[i])) {
                        valueAsInt -= 500/amountOutputsOpen;
                        controlElement[0].value = valueAsInt;
                        controlElement.trigger('change');
                    }
                }
            }
            window.setTimeout(function() { growUp(event, nextClasses, previousClasses) }, figure.speed);
        });
    },

    getValue: function() {
        return this.element.text() + ' of ' + this.capacity + ' ' + this.unit;
    }

});
